package ui;

import controller.EmployeeController;
import project_package.Employee;

import java.awt.Component;
import java.awt.event.*;
import java.util.Map;
import java.util.Set;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UnsupportedLookAndFeelException;

public class EmployeeUI implements ActionListener, ListSelectionListener 
{
	private EmployeeController empCtrl;	
	private JFrame frame;
	private JPanel addEmployee, editEmployee;
	private JTabbedPane tabbedPane;
	private static EmployeeUI instance = null;
	
	private JButton addEmployeeButtonAdd;
	private JButton addEmployeeButtonReset;
	private JButton addEmployeeButtonCloseWindow;
	private JButton addEmployeeButtonExit;
	private JComboBox<String> addEmployeeComboBoxStatus;
	private JTextField addEmployeeTextFieldName;
	private JTextField addEmployeeTextFieldSalary; 
	private JTextField addEmployeeTextFieldPhone;
	private JTextField addEmployeeTextFieldAddress;
	private JTextField addEmployeeTextFieldCPR;
	private JPasswordField addEmployeePasswordFieldPassword;
	private JPasswordField addEmployeePasswordFieldPasswordAgain;
	
	private JScrollPane editEmployeeSP;
	private JList<String> editEmployeeList;
	private DefaultListModel<String> listModel;
	private JButton editEmployeeButtonUpdate;
	private JButton editEmployeeButtonDelete;
	private JButton editEmployeeButtonReset;
	private JButton editEmployeeButtonExit;
	private JButton editEmployeeButtonCloseWindow;
	private JCheckBox editEmployeeCheckBoxChangePassword;
	private JComboBox<String> editEmployeeComboBoxStatus;
	private JTextField editEmployeeTextFieldName;
	private JTextField editEmployeeTextFieldSalary;
	private JTextField editEmployeeTextFieldPhone;
	private JTextField editEmployeeTextFieldAddress;
	private JTextField editEmployeeTextFieldCPR;
	private JTextField editEmployeeTextFieldSalesNumber;
	private JTextField editEmployeeTextFieldSalesTotal;
	private JPasswordField editEmployeePasswordFieldPassword;
	private JPasswordField editEmployeePasswordFieldPasswordAgain;
	private JLabel editEmployeeLabelSinceDate;
	
	public static EmployeeUI getInstance()
    {
    	if(instance == null)
    	{
    		instance = new EmployeeUI();
    	}
    	return instance;
    }
	public void display()
	{
		frame.getContentPane().add(tabbedPane);
		tabbedPane.add("Add employee", addEmployee);
		tabbedPane.add("Edit employee", editEmployee);
		frame.setSize(510,450);
		frame.setVisible(true);
		frame.setResizable(false);
	}

	public EmployeeUI() {
		
		frame = new JFrame("Employee UI");
		tabbedPane = new JTabbedPane();
		empCtrl = new EmployeeController();
		
		fakeEmployeeGenerator();

		//**********************************
		// PAGE 1
		//**********************************
		
		addEmployee = new JPanel();

		JLabel addEmployeeLabelName = new JLabel("Employee Name");
		JLabel addEmployeeLabelSalary = new JLabel("Employee Salary");
		JLabel addEmployeeLabelPhone = new JLabel("Employee Phone");
		JLabel addEmployeeLabelPassword = new JLabel("Employee Password");
		JLabel addEmployeeLabelPasswordAgain = new JLabel("Employee Password Again");
		JLabel addEmployeeLabelStatus = new JLabel("Employee Status");
		JLabel addEmployeeLabelAddress = new JLabel("Employee Address");
		JLabel addEmployeeLabelCPR = new JLabel("Employee CPR");
		
		addEmployeeTextFieldName = new JTextField(32);
		addEmployeeTextFieldSalary = new JTextField(12);
		addEmployeeTextFieldPhone = new JTextField(16);
		addEmployeePasswordFieldPassword = new JPasswordField(16);
		addEmployeePasswordFieldPasswordAgain = new JPasswordField(16);
		addEmployeeTextFieldAddress = new JTextField(25);
		addEmployeeTextFieldCPR = new JTextField(10);
		
		addEmployeeComboBoxStatus = new JComboBox<String>();
		addEmployeeComboBoxStatus.addItem("Manager");
		addEmployeeComboBoxStatus.addItem("Worker");
		
		addEmployeeButtonAdd = new JButton("Add");
		addEmployeeButtonAdd.addActionListener(this);
		addEmployeeButtonExit = new JButton("Exit");
		addEmployeeButtonExit.addActionListener(this);
		addEmployeeButtonReset = new JButton("Reset");
		addEmployeeButtonReset.addActionListener(this);
		addEmployeeButtonCloseWindow = new JButton("Close window");
		addEmployeeButtonCloseWindow.addActionListener(this);		
		
		GroupLayout gl_addEmployee = new GroupLayout(addEmployee);
		gl_addEmployee.setHorizontalGroup(
			gl_addEmployee.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_addEmployee.createSequentialGroup()
					.addGap(101)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_addEmployee.createSequentialGroup()
							.addGroup(gl_addEmployee.createParallelGroup(Alignment.LEADING)
								.addComponent(addEmployeeLabelCPR)
								.addComponent(addEmployeeLabelAddress)
								.addComponent(addEmployeeLabelStatus)
								.addComponent(addEmployeeLabelPasswordAgain)
								.addComponent(addEmployeeLabelPassword)
								.addComponent(addEmployeeLabelPhone)
								.addComponent(addEmployeeLabelSalary)
								.addComponent(addEmployeeLabelName))
							.addGap(18)
							.addGroup(gl_addEmployee.createParallelGroup(Alignment.LEADING)
								.addComponent(addEmployeeTextFieldName, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeeTextFieldSalary, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeeTextFieldPhone, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeePasswordFieldPassword, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeePasswordFieldPasswordAgain, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeeComboBoxStatus, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeeTextFieldCPR, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeeTextFieldAddress, GroupLayout.PREFERRED_SIZE, 149, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_addEmployee.createSequentialGroup()
							.addGap(24)
							.addGroup(gl_addEmployee.createParallelGroup(Alignment.LEADING)
								.addComponent(addEmployeeButtonAdd, GroupLayout.PREFERRED_SIZE, 102, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeeButtonCloseWindow))
							.addGap(41)
							.addGroup(gl_addEmployee.createParallelGroup(Alignment.LEADING)
								.addComponent(addEmployeeButtonReset, GroupLayout.PREFERRED_SIZE, 99, GroupLayout.PREFERRED_SIZE)
								.addComponent(addEmployeeButtonExit))))
					.addContainerGap(106, Short.MAX_VALUE))
		);
		gl_addEmployee.setVerticalGroup(
			gl_addEmployee.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_addEmployee.createSequentialGroup()
					.addGap(45)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.TRAILING, false)
						.addGroup(gl_addEmployee.createSequentialGroup()
							.addComponent(addEmployeeTextFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED))
						.addGroup(gl_addEmployee.createSequentialGroup()
							.addComponent(addEmployeeLabelName)
							.addGap(9)))
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeTextFieldSalary, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addEmployeeLabelSalary, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeTextFieldPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addEmployeeLabelPhone))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeePasswordFieldPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addEmployeeLabelPassword, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeLabelPasswordAgain, GroupLayout.PREFERRED_SIZE, 14, GroupLayout.PREFERRED_SIZE)
						.addComponent(addEmployeePasswordFieldPasswordAgain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeComboBoxStatus, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addEmployeeLabelStatus))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeTextFieldAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addEmployeeLabelAddress, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeTextFieldCPR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(addEmployeeLabelCPR))
					.addGap(13)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeButtonAdd)
						.addComponent(addEmployeeButtonReset))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(gl_addEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(addEmployeeButtonExit)
						.addComponent(addEmployeeButtonCloseWindow))
					.addContainerGap(74, Short.MAX_VALUE))
		);
		gl_addEmployee.linkSize(SwingConstants.VERTICAL, new Component[] {addEmployeeTextFieldName, addEmployeeTextFieldSalary, addEmployeeTextFieldPhone});
		gl_addEmployee.linkSize(SwingConstants.HORIZONTAL, new Component[] {addEmployeeTextFieldName, addEmployeeTextFieldSalary, addEmployeeTextFieldPhone, addEmployeePasswordFieldPassword, addEmployeePasswordFieldPasswordAgain, addEmployeeTextFieldAddress, addEmployeeTextFieldCPR, addEmployeeComboBoxStatus});
		gl_addEmployee.linkSize(SwingConstants.HORIZONTAL, new Component[] {addEmployeeButtonExit, addEmployeeButtonCloseWindow, addEmployeeButtonAdd, addEmployeeButtonReset});
		addEmployee.setLayout(gl_addEmployee);
		
		//**********************************
		// PAGE 2
		//**********************************

		editEmployee = new JPanel();
		
		listModel = new DefaultListModel<String>();
		
		JLabel editEmployeeLabelName = new JLabel("Employee Name");
		JLabel editEmployeeLabelSalary = new JLabel("Employee Salary");
		JLabel editEmployeeLabelPhone = new JLabel("Employee Phone");
		JLabel editEmployeeLabelPassword = new JLabel("Employee Password");
		JLabel editEmployeeLabelPasswordAgain = new JLabel("Employee Password Again");
		JLabel editEmployeeLabelStatus = new JLabel("Employee Status");
		JLabel editEmployeeLabelAddress = new JLabel("Employee Address");
		JLabel editEmployeeLabelCPR = new JLabel("Employee CPR");
		JLabel editEmployeeLabelSalesNumber = new JLabel("Employee # Sales");
		JLabel editEmployeeLabelSalesTotal = new JLabel("Employee Sales Total");
		
		editEmployeeTextFieldName = new JTextField(32);
		editEmployeeTextFieldName.setEditable(false);
		editEmployeeTextFieldSalary = new JTextField(12);
		editEmployeeTextFieldPhone = new JTextField(16);
		editEmployeeTextFieldAddress = new JTextField(25);
		editEmployeeTextFieldCPR = new JTextField(10);
		editEmployeeTextFieldSalesNumber = new JTextField(10);
		editEmployeeTextFieldSalesNumber.setEditable(false);
		editEmployeeTextFieldSalesTotal = new JTextField(10);
		editEmployeeTextFieldSalesTotal.setEditable(false);
		
		editEmployeePasswordFieldPassword = new JPasswordField(16);
		editEmployeePasswordFieldPasswordAgain = new JPasswordField(16);
		
		editEmployeeCheckBoxChangePassword = new JCheckBox("Change password");

		editEmployeeSP = new JScrollPane();
		
		editEmployeeComboBoxStatus = new JComboBox<String>();
		editEmployeeComboBoxStatus.addItem("Manager");
		editEmployeeComboBoxStatus.addItem("Worker");
		
		editEmployeeButtonUpdate = new JButton("Update");
		editEmployeeButtonUpdate.setEnabled(false);
		editEmployeeButtonUpdate.addActionListener(this);
		editEmployeeButtonDelete = new JButton("Delete");
		editEmployeeButtonDelete.setEnabled(false);
		editEmployeeButtonDelete.addActionListener(this);
		editEmployeeButtonReset = new JButton("Reset");
		editEmployeeButtonReset.setEnabled(false);
		editEmployeeButtonReset.addActionListener(this);
		editEmployeeButtonCloseWindow = new JButton("Close window");
		editEmployeeButtonCloseWindow.addActionListener(this);
		editEmployeeButtonExit = new JButton("Exit");
		editEmployeeButtonExit.addActionListener(this);
		
		updateEmployeeList();
		
		JLabel editEmployeeLabelSince = new JLabel("Employee Since");
		
		editEmployeeLabelSinceDate = new JLabel("n/a");

		GroupLayout gl_editEmployee = new GroupLayout(editEmployee);
		gl_editEmployee.setHorizontalGroup(
			gl_editEmployee.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_editEmployee.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_editEmployee.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_editEmployee.createSequentialGroup()
							.addComponent(editEmployeeButtonDelete, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE)
							.addGap(102)
							.addComponent(editEmployeeButtonUpdate)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(editEmployeeButtonReset, GroupLayout.PREFERRED_SIZE, 71, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_editEmployee.createSequentialGroup()
							.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
								.addComponent(editEmployeeLabelPasswordAgain)
								.addComponent(editEmployeeLabelPassword))
							.addGap(32)
							.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
								.addComponent(editEmployeePasswordFieldPassword, GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
								.addComponent(editEmployeePasswordFieldPasswordAgain, GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)))
						.addGroup(gl_editEmployee.createSequentialGroup()
							.addGroup(gl_editEmployee.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_editEmployee.createSequentialGroup()
									.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
										.addComponent(editEmployeeLabelName)
										.addComponent(editEmployeeLabelSalesNumber)
										.addComponent(editEmployeeLabelSalesTotal)
										.addComponent(editEmployeeLabelStatus)
										.addComponent(editEmployeeLabelSalary)
										.addComponent(editEmployeeLabelPhone)
										.addComponent(editEmployeeLabelAddress)
										.addComponent(editEmployeeLabelCPR))
									.addGap(46))
								.addGroup(gl_editEmployee.createSequentialGroup()
									.addComponent(editEmployeeLabelSince)
									.addGap(73)))
							.addGroup(gl_editEmployee.createParallelGroup(Alignment.TRAILING)
								.addComponent(editEmployeeTextFieldSalesTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(editEmployeeTextFieldCPR, 170, 170, 170)
								.addComponent(editEmployeeTextFieldAddress, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
								.addComponent(editEmployeeComboBoxStatus, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
								.addComponent(editEmployeeTextFieldPhone, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_editEmployee.createSequentialGroup()
									.addComponent(editEmployeeTextFieldSalary, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
									.addGap(86))
								.addComponent(editEmployeeTextFieldName, GroupLayout.PREFERRED_SIZE, 170, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
									.addComponent(editEmployeeLabelSinceDate)
									.addComponent(editEmployeeTextFieldSalesNumber, GroupLayout.PREFERRED_SIZE, 86, GroupLayout.PREFERRED_SIZE))))
						.addComponent(editEmployeeCheckBoxChangePassword))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
						.addComponent(editEmployeeButtonExit, GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
						.addComponent(editEmployeeSP, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 157, GroupLayout.PREFERRED_SIZE)
						.addComponent(editEmployeeButtonCloseWindow, Alignment.TRAILING, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		gl_editEmployee.setVerticalGroup(
			gl_editEmployee.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_editEmployee.createSequentialGroup()
					.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_editEmployee.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
								.addComponent(editEmployeeSP, GroupLayout.PREFERRED_SIZE, 286, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_editEmployee.createSequentialGroup()
									.addGap(3)
									.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
										.addComponent(editEmployeeLabelName)
										.addComponent(editEmployeeTextFieldName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(7)
									.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_editEmployee.createSequentialGroup()
											.addComponent(editEmployeeLabelSalary)
											.addGap(9)
											.addComponent(editEmployeeLabelPhone)
											.addGap(13)
											.addComponent(editEmployeeLabelStatus)
											.addGap(12)
											.addComponent(editEmployeeLabelAddress)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addGroup(gl_editEmployee.createParallelGroup(Alignment.BASELINE)
												.addComponent(editEmployeeLabelCPR)
												.addComponent(editEmployeeTextFieldCPR, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
										.addGroup(gl_editEmployee.createSequentialGroup()
											.addGap(30)
											.addComponent(editEmployeeTextFieldPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(editEmployeeComboBoxStatus, GroupLayout.PREFERRED_SIZE, 17, GroupLayout.PREFERRED_SIZE)
											.addPreferredGap(ComponentPlacement.UNRELATED)
											.addComponent(editEmployeeTextFieldAddress, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(gl_editEmployee.createParallelGroup(Alignment.LEADING)
										.addComponent(editEmployeeLabelSalesTotal)
										.addComponent(editEmployeeTextFieldSalesTotal, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addGap(11)
									.addGroup(gl_editEmployee.createParallelGroup(Alignment.BASELINE)
										.addComponent(editEmployeeLabelSalesNumber)
										.addComponent(editEmployeeTextFieldSalesNumber, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addGroup(gl_editEmployee.createParallelGroup(Alignment.BASELINE)
										.addComponent(editEmployeeLabelSince)
										.addComponent(editEmployeeLabelSinceDate)))))
						.addGroup(gl_editEmployee.createSequentialGroup()
							.addGap(40)
							.addComponent(editEmployeeTextFieldSalary, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addPreferredGap(ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
					.addGroup(gl_editEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(editEmployeePasswordFieldPassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(editEmployeeLabelPassword))
					.addGap(6)
					.addGroup(gl_editEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(editEmployeePasswordFieldPasswordAgain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(editEmployeeButtonCloseWindow)
						.addComponent(editEmployeeLabelPasswordAgain))
					.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
					.addGroup(gl_editEmployee.createParallelGroup(Alignment.BASELINE)
						.addComponent(editEmployeeButtonExit)
						.addComponent(editEmployeeButtonReset)
						.addComponent(editEmployeeButtonUpdate)
						.addComponent(editEmployeeButtonDelete))
					.addContainerGap())
				.addGroup(gl_editEmployee.createSequentialGroup()
					.addContainerGap(280, Short.MAX_VALUE)
					.addComponent(editEmployeeCheckBoxChangePassword)
					.addGap(91))
		);
		gl_editEmployee.linkSize(SwingConstants.VERTICAL, new Component[] {editEmployeeLabelName, editEmployeeLabelSalary, editEmployeeLabelPhone, editEmployeeLabelStatus, editEmployeeLabelAddress, editEmployeeLabelCPR, editEmployeeLabelSalesNumber, editEmployeeLabelSalesTotal, editEmployeeTextFieldName, editEmployeeTextFieldSalary, editEmployeeTextFieldPhone, editEmployeeTextFieldAddress, editEmployeeTextFieldCPR, editEmployeeTextFieldSalesNumber, editEmployeeTextFieldSalesTotal, editEmployeeComboBoxStatus});
		gl_editEmployee.linkSize(SwingConstants.HORIZONTAL, new Component[] {editEmployeeTextFieldAddress, editEmployeeComboBoxStatus});
		
		editEmployeeList = new JList<String>(listModel);
		editEmployeeSP.setViewportView(editEmployeeList);
		editEmployeeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		editEmployeeList.setSelectedIndex(0);
		//editEmployeeList1.addListSelectionListener(this);
		editEmployeeList.setVisibleRowCount(4);
		
		editEmployeeList.addListSelectionListener(this);
		editEmployee.setLayout(gl_editEmployee);
				
	}
	
	public void fakeEmployeeGenerator() 
	{
		empCtrl.createEmployee("Test 1", "111111111", "Hanover", 9999999L, 2000.0, true, "parola", 0, 0);
		empCtrl.createEmployee("Test 2", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 3", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 4", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 5", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 6", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 7", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 8", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 9", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 10", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 11", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 12", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 13", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 14", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 15", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 16", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 17", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 18", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 19", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 20", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 21", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 22", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 23", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 24", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 25", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 26", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 27", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 28", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 29", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);
		empCtrl.createEmployee("Test 30", "222222222", "Texas", 11111111L, 5000.0, false, "dadada", 0, 0);


	}
	
	public void updateEmployeeList()
	{
		listModel.clear();
		Set<Map.Entry<Integer, Employee>> st = empCtrl.getEmployees().entrySet();
		 
    	for(Map.Entry<Integer, Employee> employee : st)	{
			listModel.addElement(employee.getValue().getName());
		}
	}
	
	public void employeeShow(String employeeName) {
		Employee employee = empCtrl.findEmployee(employeeName);
		String employeeSalary = Double.toString(employee.getSalary());
		String employeeCivilNum = String.valueOf(employee.getCivilNum());
		String employeeSalesNumber = String.valueOf(employee.getSalesNumber());
		String employeeSalesTotal = String.valueOf(employee.getSalesTotal());
		editEmployeeLabelSinceDate.setText(employee.getDate());
		editEmployeeTextFieldName.setText(employee.getName());
		editEmployeeTextFieldSalary.setText(employeeSalary);
		editEmployeeTextFieldPhone.setText(employee.getPhoneNo());
		editEmployeePasswordFieldPassword.setText("");
		editEmployeePasswordFieldPasswordAgain.setText("");
		editEmployeeCheckBoxChangePassword.setSelected(false); 	// change password
		editEmployeeTextFieldAddress.setText(employee.getAddress());
		editEmployeeTextFieldCPR.setText(employeeCivilNum);
		editEmployeeTextFieldSalesNumber.setText(employeeSalesTotal);
		editEmployeeTextFieldSalesTotal.setText(employeeSalesNumber);
		editEmployeeComboBoxStatus.setSelectedItem(employee.getIsManager() ? "Manager" : "Worker");
	}
	
	public boolean isDouble(String number) 
	{
		try {
			Double.parseDouble(number);
			return true;
		}
		catch(Exception e) {
			return false;
		}
	}
	
	public boolean isLong(String number) 
	{
		try {
			Long.parseLong(number);
			return true;
		}
		catch(Exception e) {
			return false;
		}
	}
	
    @SuppressWarnings({ "unchecked", "deprecation" })
	public void valueChanged(ListSelectionEvent e) {
    	if (e.getSource() == editEmployeeList)
    	{
    		boolean adjust = e.getValueIsAdjusting();
	        if (!adjust)
	        {
	        	editEmployeeButtonReset.setEnabled(true);
	    		editEmployeeButtonDelete.setEnabled(true);
	    		editEmployeeButtonUpdate.setEnabled(true);
	        	JList<String> editEmployeeJList1 = (JList<String>) e.getSource();
	        	int selections[] = editEmployeeJList1.getSelectedIndices();
	        	Object selectionValues[] = editEmployeeJList1.getSelectedValues();
	        	for (int i = 0; i < selections.length; i++) {
	        		employeeShow(selectionValues[i].toString());
	        	}
	        }
    	}
    }
    
	@SuppressWarnings("deprecation")
	public void actionPerformed(ActionEvent e) 
	{
		// Close window
        if((e.getSource() == addEmployeeButtonCloseWindow) || (e.getSource() == editEmployeeButtonCloseWindow))
        	frame.dispose();
        
        else if ((e.getSource() == addEmployeeButtonExit) || (e.getSource() == editEmployeeButtonExit))
        	System.exit(0);
        
        else if (e.getSource() == addEmployeeButtonAdd) 
        {
        	String passwordOne = addEmployeePasswordFieldPassword.getText(); 									// password
			String passwordTwo = addEmployeePasswordFieldPasswordAgain.getText(); 								// password again
			String employeeSalary = addEmployeeTextFieldSalary.getText();
			if (!passwordOne.equals(passwordTwo)) 
			{
				JOptionPane.showMessageDialog(addEmployee, "The two passwords don't match","Error", 0, null);
			}
			else if (!isDouble(employeeSalary) || (isDouble(employeeSalary) && (Double.parseDouble(employeeSalary) <= 0))) {
				JOptionPane.showMessageDialog(addEmployee, "The salary should be a number over zero","Error", 0, null);
			}
			else if (!isLong(addEmployeeTextFieldCPR.getText()) || (addEmployeeTextFieldCPR.getText().length() != 10))
			{
				JOptionPane.showMessageDialog(addEmployee, "The CPR should be 10 digits","Error", 0, null);
			}
			else {
				String employeeName = addEmployeeTextFieldName.getText(); 							// name
				Double employeeSalaryD = Double.parseDouble(employeeSalary); 						// salary
				String employeePhone = addEmployeeTextFieldPhone.getText(); 						// phone
				String employeeStatus = addEmployeeComboBoxStatus.getSelectedItem().toString();		// status
				String employeeAddress = addEmployeeTextFieldAddress.getText(); 					// address
				Long employeeCPR = Long.valueOf(addEmployeeTextFieldCPR.getText()); 				// CPR

				boolean success = empCtrl.createEmployee(employeeName, employeePhone, employeeAddress, employeeCPR, employeeSalaryD, (employeeStatus.equals("Manager") ? true : false), passwordOne, 0.0, 0);
				
				if (success)
					JOptionPane.showConfirmDialog(addEmployee, "Successfully created employee " + employeeName, "Create", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
				else
					JOptionPane.showConfirmDialog(addEmployee, "There was error creating the employee", "Create", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
				
				updateEmployeeList();
			}
        }
        
        else if (e.getSource() == addEmployeeButtonReset)
        {
        	addEmployeeTextFieldName.setText("");				// name
			addEmployeeTextFieldSalary.setText("");				// salary
			addEmployeeTextFieldPhone.setText("");				// phone
			addEmployeePasswordFieldPassword.setText("");		// password
			addEmployeePasswordFieldPasswordAgain.setText("");	// password again
			addEmployeeComboBoxStatus.setSelectedIndex(0);		// type of worker
			addEmployeeTextFieldAddress.setText("");			// address
			addEmployeeTextFieldCPR.setText("");				// CPR
        }
        
        else if (e.getSource() == editEmployeeButtonUpdate)
        {
        	if (!editEmployeeTextFieldName.getText().isEmpty())
			{
        		String passwordOne = editEmployeePasswordFieldPassword.getText(); 									// password
    			String passwordTwo = editEmployeePasswordFieldPasswordAgain.getText(); 								// password again
    			String employeeSalary = editEmployeeTextFieldSalary.getText();

        		if (!passwordOne.equals(passwordTwo)) 
    			{
    				JOptionPane.showMessageDialog(editEmployee, "The two passwords don't match","Error", 0, null);
    			}
    			else if (!isDouble(employeeSalary) || (isDouble(employeeSalary) && (Double.parseDouble(employeeSalary) <= 0))) {
    				JOptionPane.showMessageDialog(editEmployee, "The salary should be a number over zero","Error", 0, null);
    			}
    			else if (!isLong(editEmployeeTextFieldCPR.getText()) || (editEmployeeTextFieldCPR.getText().length() != 10))
    			{
    				JOptionPane.showMessageDialog(editEmployee, "The CPR should be 10 digits","Error", 0, null);
    			}
    			else {
					Employee employee = empCtrl.findEmployee(String.valueOf(editEmployeeTextFieldName.getText()));
					int employeeID = employee.getID();
					int employeeSalesNumber = Integer.parseInt(editEmployeeTextFieldSalesTotal.getText());
					String employeePassword = (editEmployeeCheckBoxChangePassword.isSelected() ? editEmployeePasswordFieldPassword.getText() : employee.getPassword());
					String employeeName = String.valueOf(editEmployeeTextFieldName.getText());
					String employeePhoneNo = String.valueOf(editEmployeeTextFieldPhone.getText());
					String employeeAddress = String.valueOf(editEmployeeTextFieldAddress.getText());
					long employeeDatestamp = employee.getDatestamp();
					long employeeCivilNum = Long.parseLong(editEmployeeTextFieldCPR.getText());
					double employeeSalaryD = Double.parseDouble(editEmployeeTextFieldSalary.getText());
					double employeeSalesTotal = Double.parseDouble(editEmployeeTextFieldSalesNumber.getText());
					boolean employeeStatus = (editEmployeeComboBoxStatus.getSelectedItem().equals("Worker") ? false : true);
					boolean success = empCtrl.updateEmployee(employeeID, employeeName, employeePhoneNo, employeeAddress, employeeCivilNum, employeeDatestamp, employeeSalaryD, employeeStatus, employeePassword, employeeSalesTotal, employeeSalesNumber);
					if (success)
					{
						employeeShow(employeeName);
						JOptionPane.showConfirmDialog(editEmployee, "Successfully updated employee " + editEmployeeTextFieldName.getText(), "Update", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
					}
					else
						JOptionPane.showConfirmDialog(editEmployee, "There was error updating the employee", "Update", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
					updateEmployeeList();
				}
			}
        }
        
        else if (e.getSource() == editEmployeeButtonDelete)
        {
        	if (!editEmployeeTextFieldName.getText().isEmpty())
			{
				int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to delete " + editEmployeeTextFieldName.getText() + "?", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				if (response == JOptionPane.YES_OPTION) {
					boolean success = empCtrl.deleteEmployee(editEmployeeTextFieldName.getText());
					
					if (success)
						JOptionPane.showConfirmDialog(null, "Successfully deleted employee " + editEmployeeTextFieldName.getText(), "Delete", JOptionPane.PLAIN_MESSAGE, JOptionPane.INFORMATION_MESSAGE);
					else
						JOptionPane.showConfirmDialog(null, "There was error deleting the employee", "Delete", JOptionPane.PLAIN_MESSAGE, JOptionPane.WARNING_MESSAGE);						
				}
			}
			updateEmployeeList();
        }
        
        else if (e.getSource() == editEmployeeButtonReset)
        {
        	employeeShow(editEmployeeTextFieldName.getText());
        }
	}
	
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException 
	{
		UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		EmployeeUI employeeMenu = new EmployeeUI();
		employeeMenu.display();
	}
}
